<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Customer
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * API2 class for customer (admin)
 *
 * @category   Mage
 * @package    Mage_Customer
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class ApiCenter_SalesApi_Model_Api2_Orderdata_Rest_Admin_V1 extends ApiCenter_SalesApi_Model_Api2_Orderdata_Rest
{
    /**
     * Retrieve information about specified order item
     *
     * @throws Mage_Api2_Exception
     * @return array
     */

    
    protected function _retrieve()
    {

        try{
        $vendorId  =$this->getRequest()->getParam('vendorid');
        
       $this->_loadVendor($vendorId);
        
        $mainOrderCollection = $this->_getOrderForCollection($vendorId);
        //包含确认的和未确认的
        $ToDayAllOrder =0;

        $allConfirmOrder =0;
        $isShippedOrder = 0;
        $now= time();
        
        //今天
        $start = mktime(0,0,0,date("m",$now),date("d",$now),date("Y",$now));
        $end = mktime(23,59,59,date("m",$now),date("d",$now),date("Y",$now));
        
        //一个月以前
        foreach ($mainOrderCollection->getItems() as $order) {
                $have_confirmOrders = $this->_loadChildOrder($order->getIncrementId(),$vendorId)->getData();
                $orderCreatTime =strtotime($order->getCreatedAt());
                           if(!$have_confirmOrders){
                               
                               $allConfirmOrder++;
                               if($orderCreatTime>=$start && $orderCreatTime<=$end){     
                                    $ToDayAllOrder++;
                               }
                           }else{
                               foreach ($have_confirmOrders as $_confirmOrder){
                                     if($orderCreatTime>=$start && $orderCreatTime<=$end){     
                                    $ToDayAllOrder++;
                                     }
                                   if($_confirmOrder['STATUS']=="Shipped"){
                                       $isShippedOrder++;
                                   }
                               }
                           }
                           
        }
        
        
        //订单总金额
     //   $data['salesVolum']=$this->getSalesVolum($vendorId);
        
        $data['aWeeksalesVolum']=$this->getSalesVolum(7,$vendorId);
        $data['aMonthsalesVolum']=$this->getSalesVolum(30,$vendorId);
        
       $data['todayAllOrder']=$ToDayAllOrder;
       $data['allConfirmOrder']=$allConfirmOrder;
       $data['isShippedOrder']=$isShippedOrder;
       $data['aWeekOrder']  = $this->getDayofOrder(7,$vendorId);
       $data['aMonthOrder']=$this->getDayofOrder(30,$vendorId);
       
       $data['in_sales'] = $this->getApprovalPrdouct($vendorId,"2");
       $data['in_approval'] =$this->getApprovalPrdouct($vendorId,"3,4,5");
        $res['status']=1;
        $res['info'] = $data;
        }catch(Exception $e){
            $res['status']=0;
            $res['info']=$e->getMessage();
        }
        return $res;
    }
    
    public function getDayofOrder($day,$vendorId){
        $num = 0;
        $collection = $this->_getOrderForCollection($vendorId);
       // $collection -> getSelect()->where("d.created_at BETWEEN ".date("Y-m-d H:i:s",time()-$day*24*3600)." AND ".now());
         $collection->addFieldToFilter('d.created_at', array('datetime' => true, 'from'=>Mage::app()->getLocale()->date()->sub($day,Zend_Date::DAY)->toString('YYYY-MM-dd HH:mm:ss'),'to' => Mage::app()->getLocale()->date()->sub("0",Zend_Date::DAY)->toString('YYYY-MM-dd HH:mm:ss')));
       
         foreach ($collection->getItems() as $order) {
             $have_confirmOrders = $this->_loadChildOrder($order->getIncrementId(),$vendorId)->getData();
             if(!$have_confirmOrders){
                 $num++;
             }else{
                 foreach ($have_confirmOrders as $_order){
                     $num++;
                 }
             }
         }
         return $num;
    
    }
    
   
    public function getSalesVolum($day,$vendorId){
        $grand_total = 0;
        $collection = $this->_getOrderForCollection($vendorId);
        $collection->addFieldToFilter('d.created_at', array('datetime' => true, 'from'=>Mage::app()->getLocale()->date()->sub($day,Zend_Date::DAY)->toString('YYYY-MM-dd HH:mm:ss'),'to' => Mage::app()->getLocale()->date()->sub("0",Zend_Date::DAY)->toString('YYYY-MM-dd HH:mm:ss')));
        $filter=array(
           array(
               "attribute"=>"STATUS",
               "eq" =>"Delivered",
               ),
        );
        foreach ($collection->getItems() as $order) {
            $tb_orders = $this->_loadChildOrder($order->getIncrementId(),$vendorId,$filter);
            if($tb_orders){
                foreach ($tb_orders as $tb_order){
                    $grand_total+=$tb_order->getGrandTotal();
                }
            }
        }
        
        return round($grand_total,2);
    }
     
    
    public function getApprovalPrdouct($vendorId,$status){
      $filter=array(
           array(
               "attribute"=>"approval",
               "in" =>$status,
               ),
        );
      $collection = $this->_getPrdouctForCollection($vendorId,$filter);
       return count($collection);
    }
}